package com.zuxia.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {
    @Resource
    private RedisTemplate<String, String> redisTemplate01;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private RedisTemplate<String, String> redisTemplateS;


    private String redisCode="user:phone:code:";
    private String redisKey="user:phone:code:key";
    public Boolean SendingRedisCode(String phone,String code,Long time){
        stringRedisTemplate.opsForValue().set(redisCode+phone,code,time, TimeUnit.MINUTES);
        String s = stringRedisTemplate.opsForValue().get(redisCode + phone);
        System.out.println(phone+"\n"+s);
        if("".equals(s)){
            return false;
        }else {
            return true;
        }
    }

    public void limitRedisKey(String key){
        stringRedisTemplate.opsForValue().increment(redisKey+key, 1);
        if(stringRedisTemplate.opsForValue().get(redisKey+key)==null||"".equals(stringRedisTemplate.opsForValue().get(redisKey+key))){
            stringRedisTemplate.expire(redisKey+key,1L,TimeUnit.DAYS);
        }

    }

    public String getLimitRedisKey(String key){
        return stringRedisTemplate.opsForValue().get(redisKey+key);
    }

    public String getRedisCode(String phone){
        String s = stringRedisTemplate.opsForValue().get(redisCode + phone);
        return s;
    }

    public String upShopName="shop:up:";
    public String upShopAll="shop:up:all:";

    public void setString(String key,String value,long time){
        stringRedisTemplate.opsForValue().set(key, value,time,TimeUnit.MINUTES);
    }
    public void delString(String key){
        stringRedisTemplate.delete(key);
    }

    public String getString(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    public void setString(String key,Object value){
        stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(value));
    }
    public void setString(String key,String value){
        stringRedisTemplate.opsForValue().set(key, value);
    }

    public GeoResults getEDO(String geoKey, Circle circle){
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs
                .newGeoRadiusArgs() // 创建georadius命令参数对象
                .includeDistance() // 包含距离
                .includeCoordinates() // 包含坐标
                .sortAscending() // 按距离由近及远排序
                .limit(10); // 限制返回结果为10个
        GeoResults radius = redisTemplate.opsForGeo().radius(geoKey, circle,args);
        return radius;
    }
    //******************************************************存储团购点*******************************************************
    public String geoHouseKey(){
        return "geo:house:dictionary";
    }
    public Long geoadd(String geoKey, Point point, String member){
        return redisTemplate.opsForGeo().add(geoKey, point, member);
    }
    //******************************************************拼团个数*******************************************************
    public String getGroupKey(){
        return "group:condition:";
    }

    //******************************************************订单支付*******************************************************
    public String getZFB_Key(){
        return "order:zf:";
    }
    public String getZFB_URL_Key(){
        return "order:zf:url:";
    }
}
